
package com.winning.sx.microframework.controller.busi;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.common.util.Constains;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.manage.LoginSession;
import com.winning.sx.microframework.model.message.AjaxDataResponseMessage;
import com.winning.sx.microframework.model.message.AjaxTableResponseMessage;
import com.winning.sx.microframework.service.EasyService;
import com.winning.sx.microframework.service.sysmanage.ConstService;
import com.winning.sx.microframework.service.sysmanage.RoleManageService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/busi/warehouseMain")
public class WarehouseMainController {
    @Autowired
    EasyService easyService;

    @Autowired
    ConstService service;

    @RequestMapping(value = {"/", ""})
    public ModelAndView init() throws IOException, CustomException, SQLException {
        ModelAndView mv = new ModelAndView("/busi/warehouseMain");
        List state = service.queryCount("200");
        mv.addObject("state", state);
        LoginSession login = (LoginSession) (SecurityUtils.getSubject().getSession().getAttribute("loginSession"));

        Map eqRootClassP = new HashMap();
        eqRootClassP.put("superId", "0");
        eqRootClassP.put("validFlag", "100001");
        DataTable eqRootClassS = easyService.queryList("BASE_EqClass", eqRootClassP);
        List eqRootClass = eqRootClassS.getDataRows();
        mv.addObject("eqRootClass", eqRootClass);
        List brand = service.queryCount("300");
        mv.addObject("brand", brand);
        Map map = new HashMap();
        List eqClass = easyService.querySql("SELECT " +
                "  c1.name + '-' + c2.name name, " +
                "  c2.code " +
                "FROM BASE_EqClass c1 " +
                "  RIGHT JOIN BASE_EqClass c2 ON c1.id = c2.superId " +
                "WHERE c2.validFlag = '100001' AND c1.id IS NOT NULL ", map).getDataRows();
        mv.addObject("eqClass", eqClass);
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = {"/queryEqClassList"})
    public AjaxTableResponseMessage queryEqClassList(String datas) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        Map<String, String> param = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null)
                param = objectMapper.readValue(datas, Map.class);
            DataTable all = easyService.querySql("select " +
                    " ec.id,ec.name,sum(d.count) count,sum(d.warnCount) warnCount " +
                    " from BASE_WarehouseDetail d left join BASE_EquipmentDetail e on d.equipmentId=e.id and e.validFlag='100001' " +
                    " left join BASE_EqClass ec on e.type=ec.code  where d.validFlag='100001'  " +
                    (param.get("warehouseId") == null ? "" : (" and d.warehouseId in (" + param.get("warehouseId") + ")")) +
                    " group by ec.id,ec.name", param);
            ajaxTableResponseMessage.setDatas(all.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) all.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxTableResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/queryList"})
    public AjaxTableResponseMessage queryList(String datas) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        Map<String, String> param = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null)
                param = objectMapper.readValue(datas, Map.class);
            DataTable all = easyService.querySql("select " +
                    " d.id,d.warehouseId, d.count,c1.name brandName,e.name, ec.name typeName, ec.code oneCode,c1.code brandCode,e.id model,d.unit,d.warnCount " +
                    " from BASE_WarehouseDetail d left join BASE_EquipmentDetail e on d.equipmentId=e.id " +
                    " left join BASE_Const c1 on c1.code=e.brand " +
                    "left join BASE_EqClass ec on e.type=ec.code " +
                    " where d.validFlag='100001' and ec.id=:id" +
                    (param.get("warehouseId") == null ? "" : (" and d.warehouseId in (" + param.get("warehouseId") + ")")), param);
            ajaxTableResponseMessage.setDatas(all.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) all.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxTableResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/save"})
    public AjaxDataResponseMessage insert(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                Map param = new HashMap();
                param.put("id", map.get("id"));
                param.put("equipmentId", map.get("model"));
                param.put("warehouseId", map.get("warehouseId"));
                param.put("count", map.get("count"));
                param.put("unit", map.get("unit"));
                param.put("warnCount", map.get("warnCount"));

                List has = easyService.querySql("select * from BASE_WarehouseDetail d" +
                        " where d.validFlag='100001' and d.equipmentId=:equipmentId and d.warehouseId=:warehouseId" +
                        " and d.id!=:id ", param).getDataRows();
                if (has.size() > 0) {
                    throw new Exception("此型号的数据已经存在在仓库");
                }
                easyService.save("BASE_WarehouseDetail", "id", param);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/delete"})
    public AjaxDataResponseMessage delete(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                easyService.delete("BASE_WarehouseDetail", "id", map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("删除失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }

}

